﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LDCRNewEdit.Model;
using System.Xml;
using System.IO;
using LDCRNewEdit.Utils;

namespace LDCRNewEdit.Services
{
    class BaseDataService
    {
        public static Dictionary<string, List<BaseDataFormat>> BaseDataFormatList = new Dictionary<string, List<BaseDataFormat>>();
        public static Dictionary<string, List<BaseData>> BaseDataList = new Dictionary<string, List<BaseData>>();
        public static Dictionary<string, BaseDataPath> BaseDataPathList = new Dictionary<string, BaseDataPath>();


        /// <summary>
        /// 加载初始化数据格式
        /// </summary>
        public static void InitBaseDataFormat()
        {
            //初始化数据
            foreach (string key in BaseDataPathList.Keys)
            {
                BaseDataFormatList[key] = InitDataDataFormat(BaseDataPathList[key].XmlFilename);
            }

        }

        private static List<BaseDataFormat> InitDataDataFormat(string xmlFilename)
        {
            XmlDocument dom = new XmlDocument();
            dom.Load(xmlFilename);//装载XML文档 

            XmlNode root = dom.ChildNodes[1];

            //人物数据格式
            int count = root.ChildNodes.Count;

            List<BaseDataFormat> list = new List<BaseDataFormat>(count);
            foreach (XmlNode node in root.ChildNodes)
            {
                BaseDataFormat df = new BaseDataFormat();

                df.ArrayNum = int.Parse(node.Attributes["ArrayNum"].Value);
                df.GroupNum = int.Parse(node.Attributes["GroupNum"].Value);
                df.Len = int.Parse(node.Attributes["Len"].Value);
                df.IsString = int.Parse(node.Attributes["IsString"].Value);
                df.IsName = int.Parse(node.Attributes["IsName"].Value);
                df.Type = int.Parse(node.Attributes["Type"].Value);
                df.Description = node.Attributes["Description"].Value;

                df.NodeName = node.Name;

                list.Add(df);
            }

            return list;
        }

        public static BaseData GetBaseData(string key, int index)
        {
            if (BaseDataList[key] == null || index < 0 || index >= BaseDataList[key].Count)
            {
                return null;
            }

            return BaseDataList[key][index];
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public static void SaveBaseData()
        {
            foreach (string key in BaseDataFormatList.Keys)
            {
                using (FileStream fs = new FileStream(BaseDataPathList[key].DatFilename, FileMode.Create))
                {
                    foreach (BaseData baseData in BaseDataList[key])
                    {
                        foreach (BaseDataValue data in baseData.DataList)
                        {
                            byte[] buf = Tools.StringToBytes(data.Value, data.Len, data.IsString == 1);
                            if (buf != null)
                                fs.Write(buf, 0, buf.Length);

                        }
                    }
                    fs.Flush();
                }
            }
        }
    }
}
